import Foundation

// USB Endpoint (端点) 结构体
struct UsbEndpoint {
    static let MASK_NUMBER = 0x07
    static let MASK_DIRECTION = 0x80
    static let DIRECTION_OUT = 0x00
    static let DIRECTION_IN = 0x80

    let endpointNumber: Int
    let direction: Int
    let type: Int
    let attributes: Int
    let maxPacketSize: Int
    let interval: Int

    var endpointAddress: Int {
        return endpointNumber | direction
    }

    // 从字典创建 UsbEndpoint 实例
    static func fromMap(_ map: [String: Any]) -> UsbEndpoint {
        return UsbEndpoint(
            endpointNumber: map["endpointNumber"] as! Int,
            direction: map["direction"] as! Int,
            type: map["type"] as! Int,
            attributes: map["attributes"] as? Int ?? 0,
            maxPacketSize: map["maxPacketSize"] as? Int ?? 0,
            interval: map["interval"] as? Int ?? 0
        )
    }

    // 将 UsbEndpoint 转换为字典
    func toMap() -> [String: Any] {
        return [
            "endpointNumber": endpointNumber,
            "direction": direction,
            "type": type,
            "attributes": attributes,
            "maxPacketSize": maxPacketSize,
            "interval": interval
        ]
    }
}

// USB Interface (接口) 结构体
struct UsbInterface {
    let id: Int
    let alternateSetting: Int
    let interfaceClass: Int
    let interfaceSubClass: Int
    let interfaceProtocol: Int
    let endpoints: [UsbEndpoint]

    // 从字典创建 UsbInterface 实例
    static func fromMap(_ map: [String: Any]) -> UsbInterface {
        let endpoints = (map["endpoints"] as! [[String: Any]]).map { UsbEndpoint.fromMap($0) }
        return UsbInterface(
            id: map["id"] as! Int,
            alternateSetting: map["alternateSetting"] as? Int ?? 0,
            interfaceClass: map["interfaceClass"] as? Int ?? 0,
            interfaceSubClass: map["interfaceSubClass"] as? Int ?? 0,
            interfaceProtocol: map["interfaceProtocol"] as? Int ?? 0,
            endpoints: endpoints
        )
    }

    // 将 UsbInterface 转换为字典
    func toMap() -> [String: Any] {
        return [
            "id": id,
            "alternateSetting": alternateSetting,
            "interfaceClass": interfaceClass,
            "interfaceSubClass": interfaceSubClass,
            "interfaceProtocol": interfaceProtocol,
            "endpoints": endpoints.map { $0.toMap() }
        ]
    }
}

// USB Configuration (配置) 结构体
struct UsbConfiguration {
    let id: Int
    let name: String
    let maxPower: Int
    let interfaces: [UsbInterface]

    // 从字典创建 UsbConfiguration 实例
    static func fromMap(_ map: [String: Any]) -> UsbConfiguration {
        let interfaces = (map["interfaces"] as! [[String: Any]]).map { UsbInterface.fromMap($0) }
        return UsbConfiguration(
            id: map["id"] as! Int,
            name: map["name"] as! String,
            maxPower: map["maxPower"] as! Int,
            interfaces: interfaces
        )
    }

    // 将 UsbConfiguration 转换为字典
    func toMap() -> [String: Any] {
        return [
            "id": id,
            "name": name,
            "maxPower": maxPower,
            "interfaces": interfaces.map { $0.toMap() }
        ]
    }
}

// USB Device Description (设备描述) 结构体
struct UsbDeviceDescription {
    let device: UsbDevice
    let manufacturer: String?
    let product: String?
    let serialNumber: String?

    // 从字典创建 UsbDeviceDescription 实例
    static func fromMap(_ map: [String: Any]) -> UsbDeviceDescription {
        return UsbDeviceDescription(
            device: UsbDevice.fromMap(map["device"] as! [String: Any]),
            manufacturer: map["manufacturer"] as? String,
            product: map["product"] as? String,
            serialNumber: map["serialNumber"] as? String
        )
    }

    // 将 UsbDeviceDescription 转换为字典
    func toMap() -> [String: Any] {
        return [
            "device": device.toMap(),
            "manufacturer": manufacturer as Any,
            "product": product as Any,
            "serialNumber": serialNumber as Any
        ]
    }
}

// USB设备类
class UsbDevice {
    let identifier: String
    let deviceName: String
    let manufacturerName: String?
    let productName: String?
    let serialNumber: String?
    let vendorId: Int
    let productId: Int
    let configurationCount: Int
    let configurations: [UsbConfiguration]
    
    // 构造函数
    init(
        identifier: String,
        deviceName: String,
        manufacturerName: String?,
        productName: String?,
        serialNumber: String?,
        vendorId: Int,
        productId: Int,
        configurationCount: Int,
        configurations: [UsbConfiguration]
    ) {
        self.identifier = identifier
        self.deviceName = deviceName
        self.manufacturerName = manufacturerName
        self.productName = productName
        self.serialNumber = serialNumber
        self.vendorId = vendorId
        self.productId = productId
        self.configurationCount = configurationCount
        self.configurations = configurations
    }
    
    // 从字典构造 UsbDevice 实例
    static func fromMap(_ map: [String: Any]) -> UsbDevice {
        return UsbDevice(
            identifier: map["identifier"] as! String,
            deviceName: map["deviceName"] as! String,
            manufacturerName: map["manufacturerName"] as? String,
            productName: map["productName"] as? String,
            serialNumber: map["serialNumber"] as? String,
            vendorId: map["vendorId"] as! Int,
            productId: map["productId"] as! Int,
            configurationCount: map["configurationCount"] as! Int,
            configurations: (map["configurations"] as! [[String: Any]]).map { UsbConfiguration.fromMap($0) }
        )
    }
    
    // 将 UsbDevice 转换为字典
    func toMap() -> [String: Any] {
        return [
            "identifier": identifier,
            "deviceName": deviceName,
            "manufacturerName": manufacturerName as Any,
            "productName": productName as Any,
            "serialNumber": serialNumber as Any,
            "vendorId": vendorId,
            "productId": productId,
            "configurationCount": configurationCount,
            "configurations": configurations.map { $0.toMap() }
        ]
    }
}
